perf: drop task wrapper in EventNamespace.emit_update, keep flush tick#6734
Open
Alek99 wants to merge 2 commits into
Open
perf: drop task wrapper in EventNamespace.emit_update, keep flush tick#6734Alek99 wants to merge 2 commits into
Alek99 wants to merge 2 commits into
Conversation
Contributor
Greptile SummaryThis PR simplifies state-update websocket emits while keeping the flush yield. The main changes are:
Confidence Score: 5/5This looks safe to merge.
Important Files Changed
Reviews (2): Last reviewed commit: "chore: add news fragment for #6734" | Re-trigger Greptile |
Merging this PR will not alter performance
Comparing Footnotes
|
Awaiting a freshly created task blocks the caller exactly like awaiting the coroutine directly, so the wrapper never provided the claimed non-blocking behavior -- it only added task creation/scheduling overhead (~4-7us, 2-3x) on every outgoing state update. The task wrapper did have one load-bearing side effect: it forced an event loop tick, giving the engineio writer task a chance to flush the queued packet before the caller resumes (important when a sync event handler blocks the loop right after yielding an interim update). Keep that guarantee with an explicit asyncio.sleep(0) after the emit, which is still ~2x cheaper than the task wrapper. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Sns7EuBPYvfGCxRSRZ1bUx
bab3d82 to
3763baa
Compare
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Sns7EuBPYvfGCxRSRZ1bUx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
All Submissions:
Type of change
Changes To Core Features:
EventNamespace.emit_updatewrapped the socket emit inasyncio.create_task(...)and then immediately awaited it, with a comment claiming the task "prevents the update from being blocked behind other coroutines". Awaiting a freshly created task blocks the caller exactly like awaiting the coroutine directly — the wrapper never provided any concurrency; it only added per-update task creation/scheduling overhead (plus an f-string task name with atime.time()call). Sinceemit_updateruns while the state lock is held (viamodify_state/ the event processor'semit_deltapath), that overhead was paid on every outgoing state update.The one load-bearing side effect of the wrapper (caught by
test_yield_state_update[click_yield_interim_value]on the first iteration of this PR): awaiting a fresh task forces an event-loop tick, which lets engineio's per-connection writer task flush the queued packet before the caller resumes.sio.emititself can complete without ever suspending (the packet is only put on a non-full queue), so a sync handler that blocks the loop right after yielding (e.g.yield→time.sleep(...)) would otherwise hold the interim update hostage in the write queue. The fix keeps that guarantee explicitly withawait asyncio.sleep(0)after the emit, with a regression note explaining why the tick must stay.Micro-benchmark (Python 3.13, replicating the exact await patterns with a stand-in emit coroutine, 20k iterations after warmup):
await asyncio.create_task(emit(...), name=f"...{time.time()}")await emit(...)+await asyncio.sleep(0)1.9x less overhead per outgoing update. A writer-task ordering simulation confirms the interim packet is flushed before a loop-blocking caller resumes in both the old and new versions (and is not flushed with a plain
await emit(...)alone — which is why the sleep(0) is load-bearing).Linear: ENG-10110
🤖 Generated with Claude Code
https://claude.ai/code/session_01Sns7EuBPYvfGCxRSRZ1bUx